package com.example.demo.modules.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.modules.entity.DevicePropHistoryLog;
import com.example.demo.modules.service.IDevicePropHistoryLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhengyingya
 * @since 2023-05-06
 */
@RestController
@RequestMapping("/devicePropHistoryLog")
public class DevicePropHistoryLogController {

    @Autowired
    IDevicePropHistoryLogService devicePropHistoryLogService;

//    @GetMapping("/list")
//    List<DevicePropHistoryLog> list() {
//        return devicePropHistoryLogService.list();
//    }

    @GetMapping("/list")
    List<DevicePropHistoryLog> list(@RequestParam(value = "deviceName", defaultValue = "") String deviceName,
                                    @RequestParam(value = "startDate", defaultValue = "") String startDate,
                                    @RequestParam(value = "endDate", defaultValue = "") String endDate) {

        QueryWrapper<DevicePropHistoryLog> queryWrapper = new QueryWrapper<>();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        if (!startDate.equals("") && !endDate.equals("")) {
            queryWrapper.between("gmt_create", LocalDate.parse(startDate, df).atStartOfDay(), LocalDate.parse(endDate, df).atTime(LocalTime.MAX));
        }
        queryWrapper.eq("device_name", deviceName);
        queryWrapper.orderByAsc("gmt_create");
        return devicePropHistoryLogService.list(queryWrapper);
    }
}

